package com.pdg.android.splorr.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PlayerItem extends Item<Player>{

	private static final String KEY_PREFIX_FORMAT = "item_%d_";
	private static final String SUBKEY_EQUIPPED = "equipped";
	private static final boolean DEFAULT_EQUIPPED = false;
	private boolean equipped;

	public PlayerItem(Player theData) {
		super(theData);
	}
	public PlayerItem(Player theData,Item<?> theOther) {
		super(theData,theOther);
	}

	@Override
	protected String getSubKey(String theSubKey) {
		String theResult =  getKeyPrefix()+theSubKey;
		return theResult;
	}
	private String getKeyPrefix() {
		return getData().getKeyPrefix()+String.format(KEY_PREFIX_FORMAT, getData().getItemId(this));
	}
	@Override
	public Context getContext() {
		return getData().getContext();
	}
	public boolean isEquipped() {
		return equipped;
	}
	@Override
	protected void onLoad(SharedPreferences thePreferences) {
		super.onLoad(thePreferences);
		setEquipped(thePreferences.getBoolean(getSubKey(SUBKEY_EQUIPPED), DEFAULT_EQUIPPED));
	}
	private void setEquipped(boolean isEquipped) {
		equipped = isEquipped;
	}
	@Override
	protected void onCommit(Editor theEditor) {
		super.onCommit(theEditor);
		theEditor.putBoolean(getSubKey(SUBKEY_EQUIPPED), isEquipped());
	}

}
